package com.tuling.vipes.service.impl;


import com.tuling.vipes.service.ElasticsearchDeleteService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.ElasticsearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * @创建人: Mhh
 * @创建时间: 2021/12/10
 * @描述:
 */
@Service
@Slf4j
public class ElasticsearchDeleteServiceImp implements ElasticsearchDeleteService {

//    @Autowired
//    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ElasticsearchOperations elasticsearchOperations;



    /**
     * @param clazz : 删除的哪个索引(从传入对象中的@Document注解中indexName属性获取)
     * @return java.lang.Boolean
     * @explain : 删除索引
     * @Author Mhh
     * @Date 2021/12/27 14:27
     */

    public Boolean deleteIndex(Class<?> classType) {
        IndexOperations indexOperations = elasticsearchOperations.indexOps(classType);
        if (indexOperations.exists())
            return true;
        boolean deleteFlag = indexOperations.delete();
        return deleteFlag;
    }




    /**
     * @param id         : 要删除的主键id
     * @param entityType : 删除的哪个索引的数据(从传入对象中的@Document注解中indexName属性获取)
     * @return java.lang.Boolean
     * @throws
     * @Author Mhh
     * @Date 2021/12/9 10:40
     */
    @Override
    public Boolean delete(String id, Class<?> entityType) {
        String delete = elasticsearchOperations.delete(id, entityType);
        log.info("删除成功,删除的id为:{},delete:{}",id, delete);
        return true;
    }

    /**
     * @param entity : 这个实体对象必须是被@Document注解且有indexName属性值) 以及主键必须有值,其它参数有无都没关系(和用主键id删除没区别)
     * @return java.lang.Boolean
     * @throws
     * @Author Mhh
     * @Date 2021/12/9 10:40
     */
    @Override
    public Boolean delete(Object entity) {
        String delete = elasticsearchOperations.delete(entity);
        return true;
    }

    /**
     * @param query  : 删除掉被query所搜索到的所有数据。
     * @param entity : 删除的哪个索引的数据(从传入对象中的@Document注解中indexName属性获取)
     * @return java.lang.Boolean
     * @explain :
     * @Author Mhh
     * @Date 2021/12/27 14:22
     */

    public Boolean delete(Query query, Class<?> entity) {
        elasticsearchOperations.delete(query, entity, elasticsearchOperations.getIndexCoordinatesFor(entity));
        return true;
    }


}

